package com.zhiqingchun.web.category;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.gson.Gson;
import com.zhiqingchun.dao.category.CategoryResult;
import com.zhiqingchun.dao.category.ICategoryDao;
import com.zhiqingchun.pojo.CategoryDO;

@Controller
@RequestMapping("/categoryList.html")
public class CategoryListController {

	@RequestMapping(method = RequestMethod.POST)
	public String list(ModelMap map, HttpServletRequest request, HttpServletResponse response,
			int id) {
		CategoryDO categoryDO = new CategoryDO();
		categoryDO.setId(id);
		CategoryResult categoryResult = categoryDao.list(categoryDO, null);
		if (categoryResult.isSuccess()) {
			List<CategoryDO> categoryList = categoryResult.getCategoryList();
			String json = new Gson().toJson(categoryList);
			try {
				PrintWriter out = response.getWriter();
				out.print(json);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	@Autowired
	private ICategoryDao categoryDao;

	public ICategoryDao getCategoryDao() {
		return categoryDao;
	}

	public void setCategoryDao(ICategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}
}
